import pygame, sys, math, time

class ConfigReader():

    def __init__(self):
        self.canSend = False
        self.canRun = False
        
    def reload(self, txt, other, value):  
        if self.canRun == True:
            self.canRun = False
            f = open(txt, 'r')
            self.values = f.readlines()
            f.close()

            self.value = ""
            fixedvalues = []

            for line in self.values:
                for c in line:
                    if c != "\n":
                        self.value += c
                fixedvalues += [self.value]
                self.value = ""
                
            title = fixedvalues[0]
            self.values = []

            for line in fixedvalues[1:]:
                self.value = []
                self.value = line.split('|')
                self.values += [self.value]
                
            self.name = other
            self.value = value
            
            entry = [self.name, self.value]

            inserted = False
            for index, item in enumerate(self.values):
                #print item, entry
                if not inserted:
                    if str(item[0]) == str(entry[0]):
                        # self.values.insert(index, entry)
                        item[1] = entry[1]
                        inserted = True
                 
            myStr = title + '\n'
            for item in self.values:
                myStr += str(item[0]) + '|' + str(item[1]) + '\n'
                
            f = open(txt, 'w')
            f.write(myStr)
            f.close()
    
    def read(self, txt):
            f = open(txt, 'r')
            self.values = f.readlines()
            f.close()

            value = ""
            fixedvalues = []

            for line in self.values:
                for c in line:
                    if c != "\n":
                        value += c
                fixedvalues += [value]
                value = ""
                
                
            title = fixedvalues[0]
            self.values = []

            for line in fixedvalues[1:]:
                self.value = []
                self.value = line.split('|')
                self.values += [self.value]
            
    def send(self, txt, other):
        if self.canSend == True:
            f = open(txt, 'r')
            self.values = f.readlines()
            f.close()
            self.value = ""
            fixedvalues = []

            for line in self.values:
                for c in line:
                    if c != "\n":
                        self.value += c
                fixedvalues += [self.value]
                self.value = ""
            
            title = fixedvalues[0]
            self.values = []

            for line in fixedvalues[1:]:
                self.value = []
                self.value = line.split('|')
                self.values += [self.values]
            
            for key in other:
                if keycount < len(self.values):
                    keycount += 1
                key = self.values[keycount]